<template>
  <div class="music">
    <div class="music_in">
      <!-- <img class="static" v-if="!isPlay" :src="imgUrl" alt="">
      <img class="run" v-else :src="imgUrl" alt=""> -->
      <img :src="imgUrl" alt="" :class="{run:ifPlaying,pause:ifPaused}">
      <audio id="myAudio" @play="musicPlay" @pause="musicPause" controls autoplay :src="url" @canplay="getDuration" @timeupdate="updateTime" ref="audio"></audio>
    </div>
  </div>
</template>

<script>
import { BfApi } from "@/request/api";
export default {
  data() {
    return {
      // 默认初值
      url: "https://img2.baidu.com/it/u=1611088966,2458167776&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
      ifPlaying: false,
      ifPaused: true,
    };
  },
  props: {
    imgUrl: {
      type: String,
      default() {
        return String;
      }
    },
    ids: {
      type: Number,
      default() {
        return Number;
      },
    },
    isPlay: {
      type: Boolean,
      default() {
        return Boolean;
      }
    },
  },
  watch: {
    ids: {
      handler(newValue, oldValue) {
        if (newValue !== oldValue) {
          BfApi(newValue).then((res) => {
            // console.log(res);
            this.url = res.data.data[0].url;
          });
        }
      },
    },
    currentSong() {  //监听正在播放的歌曲改变
        this.$nextTick(() => {
            this.$refs.audio.play();
            // console.log(this.$refs.audio.duration); //此时duration为NaN
            // console.log(e.target.currentTime);
        })
    },
  },
  methods: {
    // 音乐播放
    musicPlay: function () {
        // console.log("playingmusic");
        this.ifPlaying = true;
        this.ifPaused = false;
    },
    // 音乐暂停
    musicPause: function () {
        // console.log("stopingmusic");
        // this.ifPlaying = false;
        this.ifPaused = true ;
    },
    //此时可以获取到duration
    getDuration() {
      //  console.log(this.$refs.audio.duration); 
       this.duration = this.$refs.audio.duration;
    },
    //获取audio当前播放时间
    updateTime(e) {
      // console.log(e.target.currentTime);
      this.currentTime = e.target.currentTime;  
    },
  },
};
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";
.music {
  position: fixed;
  bottom: 0px;
  left: 0;
  right: 0;
  width: 100%;
  z-index: 100;
  text-align: center;
  border-top: 3px solid @red;
  .music_in {
    // background: rgba(0, 0, 0, 0.2);
    background: #e6e6e6;
    .static {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      border: 3px solid #999;
    }
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      border: 3px solid #999;
    }
    .run {
      width: 50px;
      height: 50px;
      border-radius: 50%;
      border: 3px solid #999;
      animation: rotate 3s linear infinite;
    }
    @keyframes rotate {
      0% {
          transform: rotateZ(0deg);
          /*从0度开始*/
      }
      100% {
          transform: rotateZ(360deg);
          /*360度结束*/
      }
    }
    .pause{
      animation-play-state:paused;
    }
  }
}
</style>